package com.top.ruizhong.main.cate

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.view.ViewGroup
import android.widget.ImageView
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ConvertUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.material.appbar.AppBarLayout
import com.module.lemlin.base.BaseAbstractActivity
import com.module.lemlin.custom.SpacingItemDecoration
import com.top.ruizhong.R
import com.top.ruizhong.entity.Category
import com.top.ruizhong.main.goods.GoodsDetailsActivity
import com.top.ruizhong.main.host.SearchActivity
import com.top.ruizhong.popup.GoodsScreenPopup
import kotlinx.android.synthetic.main.activity_category_goods.*
import kotlinx.android.synthetic.main.include_goods_bar.*
import kotlinx.android.synthetic.main.include_goods_screen.*

class CateGoodsActivity : BaseAbstractActivity(), AppBarLayout.OnOffsetChangedListener {


    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, CateGoodsActivity::class.java)
            activity.startActivity(intent)
        }
    }

    private val cateIconAdapter =
        object : BaseQuickAdapter<Category, BaseViewHolder>(R.layout.item_category_icon) {
            override fun convert(holder: BaseViewHolder, item: Category) {
                holder.setText(R.id.tvCategoryTitle, item.title)
                holder.getView<ImageView>(R.id.ivCategoryIcon).setImageResource(item.icon)
            }
        }

    private val cateGoodsAdapter =
        object : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_goods_category) {
            override fun convert(holder: BaseViewHolder, item: String) {
            }
        }

    private val goodsScreenPopup: GoodsScreenPopup by lazy {
        GoodsScreenPopup(context = this, onConfirm = {})
    }

    override val layoutResId: Int
        get() = R.layout.activity_category_goods


    override fun stateBar(): StatusBar {
        return StatusBar(
            isLightMode = true,
            color = R.color.colorWhite,
            view = includeGoodsCateBar
        )
    }

    override fun initView() {

        ivGoodsBarBack.setOnClickListener {
            finish()
        }

        BarUtils.addMarginTopEqualStatusBarHeight(appBarGoodsCate)
        //为AppBar设置滑动监听
        appBarGoodsCate.addOnOffsetChangedListener(this)

        //初始化分类列表视图
        rvGoodsCateIcon.adapter = cateIconAdapter

        //初始化物品列表视图
        rvGoodsCateBody.apply {
            val decoration = SpacingItemDecoration(
                vGap = ConvertUtils.dp2px((15f)),
                hGap = ConvertUtils.dp2px((15f))
            )
            addItemDecoration(decoration)
        }.adapter = cateGoodsAdapter.apply {
            setOnItemClickListener { _, _, _ ->
                GoodsDetailsActivity.open(activity = this@CateGoodsActivity)
            }
        }

        llGoodsScreenScreen.setOnClickListener {
            goodsScreenPopup.setFillData(includeGoodsCateSort)
            goodsScreenPopup.showPopupWindow(includeGoodsCateSort)
        }

        flGoodsSearch.setOnClickListener {
            SearchActivity.open(activity = this)
        }
    }

    override fun initData() {

        val category1: MutableList<Category> = mutableListOf(
            Category(R.mipmap.ic_test_10, "洗发沐浴"),
            Category(R.mipmap.ic_test_11, "生活用品"),
            Category(R.mipmap.ic_test_12, "水果生鲜"),
            Category(R.mipmap.ic_test_13, "酒水饮料"),
            Category(R.mipmap.ic_test_14, "米面粮油")
        )
        cateIconAdapter.setNewInstance(category1)

        val goods: MutableList<String> = mutableListOf()
        for (index in 0..10) {
            goods.add(index.toString())
        }
        cateGoodsAdapter.setNewInstance(goods)
    }


    //搜索布局展开时顶部距离
    private val searchMaxTopMargin: Float =
        ConvertUtils.dp2px(45f).toFloat()

    //搜索布局关闭时顶部距离
    private val searchMinTopMargin: Float =
        ConvertUtils.dp2px(4.5f).toFloat()

    //搜索布局展开时的宽度
    private val searchMaxWidth: Float =
        (com.blankj.utilcode.util.ScreenUtils.getScreenWidth() - ConvertUtils.dp2px(26f)).toFloat()

    //搜索布局关闭时的宽度
    private val searchMinWidth: Float =
        (com.blankj.utilcode.util.ScreenUtils.getScreenWidth() - ConvertUtils.dp2px(55f)).toFloat()
    private val alphaMaxHeight: Float =
        ConvertUtils.dp2px(10f).toFloat()

    override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
        val offset: Float = verticalOffset * -0.65f

        var searchNewTopMargin: Float = searchMaxTopMargin - (offset * 1.5f)
        var searchNewWidth: Float = searchMaxWidth - (offset * 1.15f)
        val alphaNewHeight: Float = alphaMaxHeight - (offset * 0.5f)
        //处理布局的边界问题
        if (searchNewTopMargin < searchMinTopMargin) {
            searchNewTopMargin = searchMinTopMargin
        }
        if (searchNewWidth < searchMinWidth) {
            searchNewWidth = searchMinWidth
        }

        var alpha2: Float =
            255 * (alphaMaxHeight - alphaNewHeight) / alphaMaxHeight
        if (alpha2 > 255)
            alpha2 = 255f
        clGoodsSearch.setBackgroundColor(
            Color.argb(alpha2.toInt(), 255, 255, 255)
        )

        flGoodsSearch.layoutParams =
            (flGoodsSearch.layoutParams as ViewGroup.MarginLayoutParams).apply {
                topMargin = searchNewTopMargin.toInt()
                width = searchNewWidth.toInt()
            }
    }
}